/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import React, {FC, ReactNode, useEffect, useState} from 'react';
import {Dimensions, PixelRatio, View} from 'react-native';
import {AvoidArea, AvoidAreaType} from '@hadss/react_native_avoid_area/src/turbo/NativeAvoidModule';
import {Avoid} from '@hadss/react_native_avoid_area/src/index';

interface AvoidContainerProps {
  children: ReactNode;
}

const AvoidContainer: FC<AvoidContainerProps> = ({children}): ReactNode => {
  const [topHeight, setTopHeight] = useState<number>(0);
  const [bottomHeight, setBottomHeight] = useState<number>(0);
  const [leftWidth, setLeftWidth] = useState<number>(0);
  const [rightWidth, setRightWidth] = useState<number>(0);

  useEffect((): (() => void) => {
    const pixelRatio = PixelRatio.get() ? PixelRatio.get() : 1;
    const getAvoidArea = (): void => {
      let avoidArea = Avoid.getWindowAvoidArea(AvoidAreaType.TYPE_SYSTEM);
      let topHeight = avoidArea.topRect.height / pixelRatio;
      setTopHeight(topHeight);
      calcBottomHeight();
    };

    getAvoidArea();

    Avoid.addAvoidAreaListener((data) => {
      // 动态避让挖孔区
      if (data.type === AvoidAreaType.TYPE_CUTOUT) {
        getCutoutArea(data.area);
      }
    });

    return (): void => {
      Avoid.removeAvoidAreaListener();
    };
  }, []);

  const calcBottomHeight = (): void => {
    const pixelRatio = PixelRatio.get() ? PixelRatio.get() : 1;
    let avoidArea = Avoid.getWindowAvoidArea(AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
    let bottomHeight = avoidArea.bottomRect.height / pixelRatio;
    setBottomHeight(bottomHeight);
  };

  const getCutoutArea = (avoidArea: AvoidArea): void => {
    const windowWidth = Dimensions.get('window').width;
    const pixelRatio = PixelRatio.get() ? PixelRatio.get() : 1;
    if (avoidArea.leftRect.height) {
      let margin = (avoidArea.leftRect.left + avoidArea.leftRect.width) / pixelRatio;
      setLeftWidth(margin);
    } else {
      setLeftWidth(0);
    }

    if (avoidArea.rightRect.height) {
      let margin = windowWidth - avoidArea.rightRect.left / pixelRatio;
      setRightWidth(margin);
    } else {
      setRightWidth(0);
    }
  };

  return (
    <View
      style={{
        width: '100%',
        height: '100%',
        paddingTop: topHeight,
        paddingBottom: bottomHeight,
        paddingLeft: leftWidth,
        paddingRight: rightWidth,
      }}>
      {children}
    </View>
  );
};

export default AvoidContainer;
